package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;

import matrix.GaussElimPiv;
import matrix.Matrix;
import matrix.SSLE;
import matrix.Utils;

import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;
import org.scilab.forge.jlatexmath.TeXIcon;

import test.Benchmark;

public class LatexDisplay extends JPanel {
	private static final long serialVersionUID = 6712195401131729351L;
	
	public JPanel titlePnl;
	public JScrollPane scrlPane;
	public JPanel latexPnl;
	
	private TeXIcon tiTitle, tiContent;
	public JButton btnE;
	
	public static void main(String[] args) throws Exception {
		Matrix a = Benchmark.fileToMatrix("a.txt");
		Matrix b = Benchmark.fileToMatrix("b.txt");
		SSLE ss = new GaussElimPiv();
		Benchmark.bench(ss, a, b, true, Utils.TYPE_FLOAT, false);
		//
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		JFrame f = new JFrame();
		LatexDisplay d = new LatexDisplay("Problem");
		d.setContent(ss.steps().toString());
		f.setContentPane(d);
		f.setVisible(true);
		f.pack();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public LatexDisplay(String s) {
		setTitle(s);
		setContent("");
		init();
	}
	
	public void setTitle(String title) {
		tiTitle = new TeXFormula(title).createTeXIcon(TeXConstants.STYLE_DISPLAY, 14);
	}
	
	public void setContent(String con) {
		tiContent = new TeXFormula(con).createTeXIcon(TeXConstants.STYLE_DISPLAY, 14);
		repaint();
	}
	
	private void init() {
		setFocusable(false);
		setSize(new Dimension(512, 512));
		// title
		tiTitle.isColored = true;
		tiTitle.setForeground(Color.black);
		titlePnl = new JPanel() {
			private static final long serialVersionUID = -3548410603067664242L;
			
			@Override
			public void paint(Graphics g) {
				super.paint(g);
				tiTitle.paintIcon(this, g, 4, 4);
			}
		};
		titlePnl.setPreferredSize(new Dimension(10, 24));
		titlePnl.setBorder(UIManager.getBorder("ComboBox.border"));
		titlePnl.setFocusable(false);
		//
		scrlPane = new JScrollPane();
		scrlPane.setFocusable(false);
		scrlPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		
		latexPnl = new JPanel() {
			private static final long serialVersionUID = -3707732936831660002L;
			
			@Override
			public void paint(Graphics g) {
				super.paint(g);
				tiContent.paintIcon(this, g, 0, 0);
				setPreferredSize(new Dimension(tiContent.getIconWidth(), tiContent.getIconHeight()));
			}
		};
		latexPnl.setFocusable(false);
		latexPnl.setBackground(Color.WHITE);
		scrlPane.setViewportView(latexPnl);
		GroupLayout gl_latexPnl = new GroupLayout(latexPnl);
		gl_latexPnl.setHorizontalGroup(gl_latexPnl.createParallelGroup(Alignment.LEADING).addGap(0, 510, Short.MAX_VALUE));
		gl_latexPnl.setVerticalGroup(gl_latexPnl.createParallelGroup(Alignment.LEADING).addGap(0, 480, Short.MAX_VALUE));
		latexPnl.setLayout(gl_latexPnl);
		
		btnE = new JButton("E");
		btnE.setFocusable(false);
		btnE.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_btnE_actionPerformed(arg0);
			}
		});
		
		JButton button = new JButton("");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				toggleCollapse();
			}
		});
		setLayout(new BorderLayout(0, 0));
		
		GroupLayout gl_titlePnl = new GroupLayout(titlePnl);
		gl_titlePnl.setHorizontalGroup(gl_titlePnl.createParallelGroup(Alignment.TRAILING).addGroup(
				gl_titlePnl.createSequentialGroup().addContainerGap(432, Short.MAX_VALUE).addComponent(btnE).addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(button)));
		gl_titlePnl.setVerticalGroup(gl_titlePnl.createParallelGroup(Alignment.LEADING).addGroup(
				gl_titlePnl
						.createSequentialGroup()
						.addGroup(
								gl_titlePnl.createParallelGroup(Alignment.LEADING).addComponent(button, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
										.addComponent(btnE)).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		titlePnl.setLayout(gl_titlePnl);
		add(titlePnl, BorderLayout.NORTH);
		add(scrlPane);
	}
	
	protected void do_btnE_actionPerformed(ActionEvent arg0) {
		JFileChooser fc = new JFileChooser("export");
		if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
			try {
				BufferedImage im = new BufferedImage(tiContent.getIconWidth(), tiContent.getIconHeight(), BufferedImage.TYPE_INT_RGB);
				latexPnl.paint(im.getGraphics());
				ImageIO.write(im, "png", new File(fc.getSelectedFile().getAbsolutePath() + ".png"));
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "Couldn't export image.");
			}
		}
	}
	
	public void toggleCollapse() {
		scrlPane.setVisible(!scrlPane.isVisible());
		if (scrlPane.isVisible())
			scrlPane.getParent().setPreferredSize(new Dimension(scrlPane.getPreferredSize().width, scrlPane.getParent().getPreferredSize().height + scrlPane.getHeight()));
		else
			scrlPane.getParent().setPreferredSize(new Dimension(scrlPane.getPreferredSize().width, scrlPane.getParent().getPreferredSize().height - scrlPane.getHeight()));
		Component c = scrlPane;
		do
			c.revalidate();
		while ((c = c.getParent()) != null);
	}
	
	public boolean isCollapsed() {
		return scrlPane.isVisible();
	}
}
